#include <iostream>
#include <vector>
#include <functional>
#include <unordered_map>
#include <memory>

// 事件类型枚举
enum class EventType {
    PRICE_UPDATE,
    ORDER_FILLED
};

// 事件基类
class Event {
public:
    virtual EventType getType() const = 0;
};

// 价格更新事件
class PriceUpdateEvent : public Event {
public:
    PriceUpdateEvent(double newPrice) : newPrice(newPrice) {}

    EventType getType() const override {
        return EventType::PRICE_UPDATE;
    }

    double newPrice;
};

// 订单成交事件
class OrderFilledEvent : public Event {
public:
    OrderFilledEvent(int orderId) : orderId(orderId) {}

    EventType getType() const override {
        return EventType::ORDER_FILLED;
    }

    int orderId;
};

// 事件处理器基类
class EventHandler {
public:
    virtual void handleEvent(const Event& event) = 0;
};

// 价格更新事件处理器
class PriceUpdateEventHandler : public EventHandler {
public:
    void handleEvent(const Event& event) override {
        const PriceUpdateEvent& priceEvent = static_cast<const PriceUpdateEvent&>(event);
        std::cout << "价格更新到: " << priceEvent.newPrice << std::endl;
    }
};

// 订单成交事件处理器
class OrderFilledEventHandler : public EventHandler {
public:
    void handleEvent(const Event& event) override {
        const OrderFilledEvent& orderEvent = static_cast<const OrderFilledEvent&>(event);
        std::cout << "订单 " << orderEvent.orderId << " 已成交" << std::endl;
    }
};

// 事件驱动管理器
class EventDispatcher {
public:
    // 注册事件处理器
    void registerHandler(EventType type, std::unique_ptr<EventHandler> handler) {
        handlers[type] = std::move(handler);
    }

    // 分发事件
    void dispatchEvent(const Event& event) {
        auto it = handlers.find(event.getType());
        if (it!= handlers.end()) {
            it->second->handleEvent(event);
        }
    }

private:
    // 存储事件类型和对应处理器的映射
    std::unordered_map<EventType, std::unique_ptr<EventHandler>> handlers;
};


int main() {
    EventDispatcher dispatcher;

    // 注册价格更新事件处理器
    dispatcher.registerHandler(EventType::PRICE_UPDATE, std::make_unique<PriceUpdateEventHandler>());
    // 注册订单成交事件处理器
    dispatcher.registerHandler(EventType::ORDER_FILLED, std::make_unique<OrderFilledEventHandler>());

    // 模拟价格更新事件
    PriceUpdateEvent priceEvent(123.45);
    dispatcher.dispatchEvent(priceEvent);

    // 模拟订单成交事件
    OrderFilledEvent orderEvent(1001);
    dispatcher.dispatchEvent(orderEvent);

    return 0;
}